import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class ValueShadowSelectForm extends StatelessWidget {
  final VoidCallback onTap;
  final String title;
  final String value;
  final Color valueColor;
  final bool hasError;
  final String errorText;

  const ValueShadowSelectForm({
    Key key,
    this.onTap,
    this.valueColor,
    this.title,
    this.value,
    this.hasError,
    this.errorText,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String _value = value;
    if (!FormEditStatusInheritedWidget.ofEnable(context) && value == null) {
      _value = '';
    }
    return Stack(
      children: [
        Container(
          constraints: BoxConstraints(
              minHeight: getDp(60),
              minWidth: MediaQuery.of(context).size.width),
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.25),
                blurRadius: getDp(22),
                spreadRadius: -getDp(10),
              ),
            ],
            borderRadius: BorderRadius.circular(getDp(15)),
          ),
          margin: EdgeInsets.symmetric(
            horizontal: getDp(25),
          ),
          child: PressedOpacityButton(
            onPressed:
                FormEditStatusInheritedWidget.ofEnable(context) ? onTap : () {},
            disabledColor: Colors.white,
            padding: EdgeInsets.only(
              left: getDp(15),
            ),
            child: _value == null
                ? _buildSelectMode()
                : _buildValueMode(context, _value),
          ),
        ),
        if (hasError == true)
          Positioned(
            top: getDp(14),
            right: getDp(18),
            child: Text(
              errorText ?? S.current.number_is_taken__please_enter_again,
              style: TextStyle(
                color: aceRedColor,
                fontSize: getSp(12),
                fontWeight: FontWeight.w400,
                height: 14 / 12,
              ),
            ),
          ),
      ],
    );
  }

  Column _buildValueMode(BuildContext context, String _value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          title,
          style: TextStyle(
            color: aceDarkGrayColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w400,
            height: 14 / 16,
          ),
        ),
        SizedBox(
          height: getDp(6),
        ),
        Text(
          _value,
          style: FormEditStatusInheritedWidget.ofStyle(context, onTap == null)
              .copyWith(
            color: valueColor,
          ),
        ),
        SizedBox(
          height: getDp(4),
        ),
      ],
    );
  }

  Row _buildSelectMode() {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
            color: aceGrayColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w700,
            height: 14 / 16,
          ),
        ),
        SizedBox(
          width: getDp(10),
        ),
        Image.asset(
          A.assetsImagesDashboardExpenseRight,
          width: getDp(10),
          height: getDp(14),
          fit: BoxFit.contain,
        ),
      ],
    );
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(StringProperty('value', value));
    properties.add(StringProperty('title', title));
    properties.add(ObjectFlagProperty<VoidCallback>.has('onTap', onTap));
    properties.add(ColorProperty('valueColor', valueColor));
  }
}
